import java.util.HashMap;
import java.util.Set;

class Response extends HashMap<String, String>
{
  private static final long serialVersionUID = 1L;
  
  private int code;
  private String reason;
  private byte[] bytes;

  public Response()
  {
	System.out.println("Response, Response()");
    addHeader("Date", new MyDate().getCurrentDate());
    addHeader("Server", "DeWebServerT");
    //addHeader("Keep-Alive", "timeout=15, max=100");
    //addHeader("Connection", "keep-alive");
  }

  public void setCODE(int code)
  {
	System.out.println("Response, setCODE(int code)");
    this.code = code;
  }

  public void setREASON(String reason)
  {
	System.out.println("Response, setREASON(String reason)");
    this.reason = reason;
  }

  public void setBYTES(byte[] bytes)
  {
	System.out.println("Response, setBYTES(bytte[] bytes)");
    this.bytes = bytes;
  }

  public int getCode()
  {
	System.out.println("Response, getCode()");
    return code;
  }

  public byte[] getBytes()
  {
	System.out.println("Response, getBytes()");
	return bytes;
   /* if (bytes.length > 0)
    {
      String statusline = this.toString();
      statusline += "\r\n";
      byte[] statuslinebytes = statusline.getBytes();
      
      byte[] b = new byte[statuslinebytes.length + bytes.length];
      System.arraycopy(statuslinebytes, 0, b, 0, statuslinebytes.length);
      System.arraycopy(bytes, 0, b, statuslinebytes.length, bytes.length);
      return b;
    }
    else
    {
      String statusline = "HTTP/1.1 " + code + " " + reason;
      return statusline.getBytes();
    }*/
  }
  
  public void addHeader(String header, String message)
  {
	System.out.println("Response, addHeader(String header, String message)");
    this.put(header, message);
  }
  
  public void finalize() throws Throwable
  {
	System.out.println("Response, finalize()");
	this.clear();
  }
  
  public String getStatusLine() {
	  return "HTTP/1.1 " + code + " " + reason + "\r\n";
  }
  
  public String getHeaders() {
	  Set<String> headersSet = this.keySet(); 
	  Object headers[] = headersSet.toArray();
	  String header = "";
	    for (int i = 0; i < headers.length; i++)
	    {
	       header += headers[i] + ": " + this.get(headers[i]) + "\r\n";
	    }
	    return header + "\r\n";
  }
  
  public String toString()
  {
	System.out.println("Response, toString()");
    //String header = "HTTP/1.1 " + code + " " + reason + "\r\n";
    //Set<String> headersSet = this.keySet(); 
    //Object headers[] = headersSet.toArray();
    //for (int i = 0; i < headers.length; i++)
    //{
      // header += headers[i] + ": " + this.get(headers[i]) + "\r\n";
    //}
	
    return getStatusLine()+getHeaders()+new String(getBytes());
  }
}